package com.xgz.service.impl;

import com.xgz.dao.TUserDao;
import com.xgz.entity.TUser;
import com.xgz.service.TUserService;
import com.xgz.util.MD5Util;
import jdk.nashorn.internal.ir.CallNode;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class TUserServiceImpl implements TUserService {
    @Autowired
    private TUserDao tUserDao;

    /**
     * 注册
     * @param tUser
     * @return
     */
    @Override
    public int addUser(TUser tUser) {
        //获取盐值
        String salt = UUID.randomUUID().toString();
        //对密码进行加密
        String newPassword = MD5Util.MD5EncodeUtf8(tUser.getPassword(), salt);
        //赋值
        tUser.setPassword(newPassword);
        tUser.setSalt(salt);
        //调用dao接口方法
        int i = tUserDao.addUser(tUser);
        return i;
    }

    /**
     * 登录
     * @param tUser
     * @return
     */
    @Override
    public boolean queryUser(TUser tUser) {
        //调用到接口方法
        List<TUser> tUsers = tUserDao.queryByUserName(tUser.getUsername());
        //判断是否查询到结果
        if (tUsers.size() != 0){
            //获取数据库里密码
            String password1 = tUsers.get(0).getPassword();
            //获取盐值
            String salt = tUsers.get(0).getSalt();
            //加密从前台接收来的密码
            String lopass = MD5Util.MD5EncodeUtf8(tUser.getPassword(), salt);
            //判断两个密码是否一致
            if (password1.equals(lopass)){
                return true;
            }else{
                return false;
            }
        }
       return false;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<TUser> queryAll() {
        return tUserDao.queryAll();
    }

    /**
     * 根据id查询
     * @param
     * @return
     */
    @Override
    public boolean queryById(Integer id) {
        List<TUser> tUsers = tUserDao.queryById(id);
        return tUsers.size()>0?true:false ;
    }

}
